package com.qcxk.yuqian.rtu.controller;

import org.springframework.web.bind.annotation.RequestMapping;

import com.qcxk.yuqian.rtu.entity.Files;
import com.qcxk.yuqian.system.utils.RedisUtil;

import com.alibaba.fastjson.JSONObject;
import com.qcxk.yuqian.rtu.service.IFilesService;
import com.qcxk.yuqian.system.component.behaviour.BehaviourOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcxk.yuqian.system.component.constant.SystemConstant;

import javax.servlet.http.HttpServletRequest;

import java.lang.reflect.Field;
import java.util.List;

/**
 * @auther lp
 * @create 2021-05-29 05:40:57
 * @describe 前端控制器
 */
@RestController
@RequestMapping("/files")
public class FilesController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private IFilesService filesService;

    @BehaviourOperator(desc = "查询Files信息")
    @RequestMapping(value = "/loadFiles")
    public JSONObject loadFiles(Files files) {
        JSONObject json = new JSONObject();
        files = filesService.getById(files.getId());
        if (files != null) {
            json.put("code", "0");
            json.put("data", files);
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "查询Files列表")
    @RequestMapping(value = "/queryFilesList")
    public JSONObject queryFilesList(Page page, Files files) {
        JSONObject json = new JSONObject();
        page = filesService.queryFilesPage(page, files);
        json.put("code", "0");
        json.put("current", page.getCurrent());
        json.put("size", page.getSize());
        json.put("total", page.getTotal());

        List<Files> list = page.getRecords();

        json.put("dataList", list);
        return json;
    }

    @BehaviourOperator(desc = "单一属性修改Files")
    @RequestMapping(value = "/updateFilesSingle")
    public JSONObject updateFilesSingle(Integer id, String column, String value) throws NoSuchFieldException, IllegalAccessException {
        JSONObject json = new JSONObject();
        Files files = filesService.getById(id);
        Class<? extends Files> clazz = files.getClass();// 获取到对象对应的class对象
        Field nameField = clazz.getDeclaredField(column);// 获取私有成员变量:column
        nameField.setAccessible(true);// 设置操作权限为true
        nameField.set(files, value);
        if (filesService.updateById(files)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "整体修改Files")
    @RequestMapping(value = "/updateFilesMultiple")
    public JSONObject updateFilesMultiple(Files files) {
        JSONObject json = new JSONObject();

        if (filesService.updateById(files)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "删除Files")
    @RequestMapping(value = "/deleteFilesById")
    public JSONObject deleteFilesById(Files files) {
        JSONObject json = new JSONObject();
        if (filesService.deleteFilesById(files)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "新增Files")
    @RequestMapping(value = "/addFiles")
    public JSONObject addFiles(Files files) {
        JSONObject json = new JSONObject();
        if (filesService.save(files)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

}

